#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Zhangcl
from wsgiref.simple_server import make_server


def handle_index():
    # return ['<h1> Hello,index</h1>'.encode('utf-8')]
    f = open('1.html', mode='rb')
    data = f.read()
    f.close()
    return [data, ]

def handle_date():
    return ['<h1> Hello,data</h1>'.encode('utf-8')]


ULR_DICT ={
    "/index": handle_index,
    "/date": handle_date,
}


def RunServer(environ, start_response):
    # environ 客户端发来的所有的数据
    # start_response 封装要返回给用户的数据，响应头状态
    start_response('200 OK', [('Content-Type', 'text/html')])
    # return [bytes('<h1>Hello, web!</h1>', encoding='utf-8'), ]
    current_url = environ['PATH_INFO']
    # if current_url == '/index':
    #     return handle_index()
    # elif current_url == '/data':
    #     return handle_data()
    # else:
    #     return ['<h1>404</h1>'.encode('utf-8')]
    func = None
    if current_url in ULR_DICT:
        func = ULR_DICT[current_url]
    if func:
        return func()
    else:
        return ['<h1>404</h1>'.encode('utf-8')]

if __name__ == '__main__':
    httpd = make_server('', 8888, RunServer)
    print("Serving HTTP on port 8888...")
    httpd.serve_forever()